import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
var _excluded = ["content", "numberFormatOptions", "numberPopoverRender"],
    _excluded2 = ["text", "mode", "render", "renderFormItem", "fieldProps", "proFieldKey", "plain", "valueEnum", "placeholder", "locale", "customSymbol", "numberFormatOptions", "numberPopoverRender"];
import { jsx as _jsx } from "react/jsx-runtime";
import { intlMap as allIntlMap, useIntl } from '@ant-design/pro-provider';
import { InputNumber, Popover } from 'antd-v4';
import omit from 'omit.js';
import useMergedState from "rc-util/es/hooks/useMergedState";
import React, { useMemo } from 'react';
var defaultMoneyIntl = new Intl.NumberFormat('zh-Hans-CN', {
  currency: 'CNY',
  style: 'currency'
});
var enMoneyIntl = {
  style: 'currency',
  currency: 'USD'
};
var ruMoneyIntl = {
  style: 'currency',
  currency: 'RUB'
};
var rsMoneyIntl = {
  style: 'currency',
  currency: 'RSD'
};
var msMoneyIntl = {
  style: 'currency',
  currency: 'MYR'
};
var ptMoneyIntl = {
  style: 'currency',
  currency: 'BRL'
};
var intlMap = {
  default: defaultMoneyIntl,
  'zh-Hans-CN': {
    currency: 'CNY',
    style: 'currency'
  },
  'en-US': enMoneyIntl,
  'ru-RU': ruMoneyIntl,
  'ms-MY': msMoneyIntl,
  'sr-RS': rsMoneyIntl,
  'pt-BR': ptMoneyIntl
};

var getTextByLocale = function getTextByLocale(localeStr, paramsText, precision, config) {
  var moneyText = paramsText;

  if (typeof moneyText === 'string') {
    moneyText = Number(moneyText);
  }

  if (!moneyText && moneyText !== 0) return '';
  return new Intl.NumberFormat(localeStr || 'zh-Hans-CN', _objectSpread(_objectSpread({}, intlMap[localeStr || 'zh-Hans-CN'] || intlMap['zh-Hans-CN']), {}, {
    maximumFractionDigits: precision
  }, config)).format(moneyText);
};

var DefaultPrecisionCont = 2;
var InputNumberPopover = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
  var content = _ref.content,
      numberFormatOptions = _ref.numberFormatOptions,
      numberPopoverRender = _ref.numberPopoverRender,
      rest = _objectWithoutProperties(_ref, _excluded);

  var _useMergedState = useMergedState(function () {
    return rest.defaultValue;
  }, {
    value: rest.value,
    onChange: rest.onChange
  }),
      _useMergedState2 = _slicedToArray(_useMergedState, 2),
      value = _useMergedState2[0],
      onChange = _useMergedState2[1];

  var dom = content === null || content === void 0 ? void 0 : content(_objectSpread(_objectSpread({}, rest), {}, {
    value: value
  }));
  var props = {
    visible: dom ? rest.visible : false
  };
  return _jsx(Popover, _objectSpread(_objectSpread({
    placement: "topLeft"
  }, props), {}, {
    trigger: ['focus', 'click'],
    content: dom,
    getPopupContainer: function getPopupContainer(triggerNode) {
      return (triggerNode === null || triggerNode === void 0 ? void 0 : triggerNode.parentElement) || document.body;
    },
    children: _jsx(InputNumber, _objectSpread(_objectSpread({
      ref: ref
    }, rest), {}, {
      value: value,
      onChange: onChange
    }))
  }));
});
/**
 * 金额组件
 *
 * @param FieldMoneyProps {
 *     text: number;
 *     moneySymbol?: string; }
 */

var FieldMoney = function FieldMoney(_ref2, ref) {
  var _fieldProps$customSym, _fieldProps$precision;

  var text = _ref2.text,
      type = _ref2.mode,
      render = _ref2.render,
      renderFormItem = _ref2.renderFormItem,
      fieldProps = _ref2.fieldProps,
      proFieldKey = _ref2.proFieldKey,
      plain = _ref2.plain,
      valueEnum = _ref2.valueEnum,
      placeholder = _ref2.placeholder,
      _ref2$locale = _ref2.locale,
      locale = _ref2$locale === void 0 ? (_fieldProps$customSym = fieldProps.customSymbol) !== null && _fieldProps$customSym !== void 0 ? _fieldProps$customSym : 'zh-Hans-CN' : _ref2$locale,
      _ref2$customSymbol = _ref2.customSymbol,
      customSymbol = _ref2$customSymbol === void 0 ? fieldProps.customSymbol : _ref2$customSymbol,
      _ref2$numberFormatOpt = _ref2.numberFormatOptions,
      numberFormatOptions = _ref2$numberFormatOpt === void 0 ? fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.numberFormatOptions : _ref2$numberFormatOpt,
      _ref2$numberPopoverRe = _ref2.numberPopoverRender,
      numberPopoverRender = _ref2$numberPopoverRe === void 0 ? (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.numberPopoverRender) || false : _ref2$numberPopoverRe,
      rest = _objectWithoutProperties(_ref2, _excluded2);

  var precision = (_fieldProps$precision = fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.precision) !== null && _fieldProps$precision !== void 0 ? _fieldProps$precision : DefaultPrecisionCont;
  var intl = useIntl(); // 当手动传入locale时，应该以传入的locale为准，未传入时则根据全局的locale进行国际化

  if (locale && allIntlMap[locale]) {
    intl = allIntlMap[locale];
  }

  var moneySymbol = useMemo(function () {
    if (customSymbol) {
      return customSymbol;
    }

    var defaultText = intl.getMessage('moneySymbol', '￥');

    if (rest.moneySymbol === false || fieldProps.moneySymbol === false) {
      return undefined;
    }

    return defaultText;
  }, [customSymbol, fieldProps.moneySymbol, intl, rest.moneySymbol]);

  if (type === 'read') {
    var dom = _jsx("span", {
      ref: ref,
      children: getTextByLocale(moneySymbol ? locale : false, text, precision, numberFormatOptions !== null && numberFormatOptions !== void 0 ? numberFormatOptions : fieldProps.numberFormatOptions)
    });

    if (render) {
      return render(text, _objectSpread({
        mode: type
      }, fieldProps), dom);
    }

    return dom;
  }

  if (type === 'edit' || type === 'update') {
    var _dom = _jsx(InputNumberPopover, _objectSpread({
      content: function content(props) {
        var _props$value, _props$value$toString;

        if (numberPopoverRender === false) return;
        if (!props.value) return;
        var reg = new RegExp("/B(?=(d{".concat(3 + (precision - DefaultPrecisionCont), "})+(?!d))/g"));
        var localeText = getTextByLocale(moneySymbol ? locale : false, (_props$value = props.value) === null || _props$value === void 0 ? void 0 : (_props$value$toString = _props$value.toString()) === null || _props$value$toString === void 0 ? void 0 : _props$value$toString.replace(reg, ','), precision, _objectSpread(_objectSpread({}, numberFormatOptions), {}, {
          notation: 'compact'
        }));

        if (typeof numberPopoverRender === 'function') {
          return numberPopoverRender === null || numberPopoverRender === void 0 ? void 0 : numberPopoverRender(props, localeText);
        }

        return localeText;
      },
      ref: ref,
      precision: precision,
      // 删除默认min={0}，允许输入一个负数的金额，用户可自行配置min来限制是否允许小于0的金额
      formatter: function formatter(value) {
        if (value && moneySymbol) {
          var reg = new RegExp("/B(?=(d{".concat(3 + (precision - DefaultPrecisionCont), "})+(?!d))/g"));
          return "".concat(moneySymbol, " ").concat(value).replace(reg, ',');
        }

        return value === null || value === void 0 ? void 0 : value.toString();
      },
      parser: function parser(value) {
        if (moneySymbol && value) {
          return value.replace(new RegExp("\\".concat(moneySymbol, "\\s?|(,*)"), 'g'), '');
        }

        return value;
      },
      placeholder: placeholder
    }, omit(fieldProps, ['numberFormatOptions', 'precision', 'numberPopoverRender', 'customSymbol'])));

    if (renderFormItem) {
      return renderFormItem(text, _objectSpread({
        mode: type
      }, fieldProps), _dom);
    }

    return _dom;
  }

  return null;
};

export default /*#__PURE__*/React.forwardRef(FieldMoney);